package com.simple01.str;

import java.util.Arrays;

/**
 * 空格转换为%20
 */
public class RemoveSpaceWord {

    public static void main(String[] args) {
        System.out.println(removeSpaceWord(new StringBuffer("hello word")));
    }

    public static String removeSpaceWord(StringBuffer str ){

        String s = str.toString();
        int count = 0 ;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ' ){
                count ++ ;
            }
        }

        char[] temp = new char[s.length() + count * 2];
        int k = temp.length - 1 ;
        for (int i = s.length() - 1 ; i >= 0  ; i--) {
            char c = s.charAt(i);
            if (c == ' '){
                temp[k] = '0' ;
                k -- ;
                temp[k] = '2' ;
                k-- ;
                temp[k] = '%';
            }else {
                temp[k] = c ;
            }
            k -- ;
        }

        return new String(temp) ;
    }
}
